<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: Transform Feedback</title>
    <meta charset="utf-8">
    <script src="../build/picogl.js"></script>
    <script src="utils/gl-matrix.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: Transform Feedback
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/transformfeedback.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="shader/vs" id="vs">
        #version 300 es

        #define SIN2 0.03489949
        #define COS2 0.99939082
        
        layout(location=0) in vec2 position;
        layout(location=1) in vec3 color;
        
        out vec2 vPosition;
        out vec3 vColor; 
        void main() {
            mat2 rotation = mat2(
                COS2, SIN2,
                -SIN2, COS2
            );

            vPosition = rotation * position;
            vColor = color;
            gl_Position = vec4(vPosition, 0.0, 1.0);
        }
    </script>
    <script type="shader/fs" id="fs">
        #version 300 es
        precision highp float;
        
        in vec3 vColor;
        
        out vec4 fragColor;
        void main() {
            fragColor = vec4(vColor, 1.0);
        }
    </script>
    
    <script type="text/javascript">
        utils.addTimerElement();

        var canvas = document.getElementById("gl-canvas");

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .clearColor(0.0, 0.0, 0.0, 1.0);

        var timer = app.createTimer();

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);
        }

        // TRANSFORM FEEDBACK PROGRAM
        var vsSource =  document.getElementById("vs").text.trim();
        var fsSource =  document.getElementById("fs").text.trim();
              
        // FINAL ARGUMENT IS TRANSFORM FEEDBACK VARYINGS
        var program = app.createProgram(vsSource, fsSource, ["vPosition"]);

        // INPUT AND OUTPUT VERTEX BUFFERS
        var positions1 = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
            -0.3, -0.3,
             0.3, -0.3,
             0.0, 0.3 
        ]));

        var positions2 = app.createVertexBuffer(PicoGL.FLOAT, 2, 6);

        // COLOR BUFFER BECAUSE IT WON'T BE PART OF TRANSFORM FEEDBACK
        var colors = app.createVertexBuffer(PicoGL.UNSIGNED_BYTE, 3, new Uint8Array([
            255, 0, 0,
            0, 255, 0,
            0, 0, 255
        ]));

        // COMBINE VERTEX BUFFERS INTO INPUT AND OUTPUT VERTEX ARRAYS
        var triangleArrayA = app.createVertexArray()
        .vertexAttributeBuffer(0, positions1)
        .vertexNormalizedAttributeBuffer(1, colors);

        var triangleArrayB = app.createVertexArray()
        .vertexAttributeBuffer(0, positions2)
        .vertexNormalizedAttributeBuffer(1, colors);

        // CREATE TRANSFORM FEEDBACK OBJECTS AND
        // INDICATE BUFFERS TO CAPTURE
        var transformFeedbackA = app.createTransformFeedback()
        .feedbackBuffer(0, positions1);

        var transformFeedbackB = app.createTransformFeedback()
        .feedbackBuffer(0, positions2);

        // CREATE DRAW CALLS FROM PROGRAM AND TRANSFORMFEEDBACK.
        // EACH DRAW CALL REPRESENTS ONE DIRECTION OF DRAW/CAPTURE
        var drawCallA = app.createDrawCall(program, triangleArrayA)
        .transformFeedback(transformFeedbackB);
        
        var drawCallB = app.createDrawCall(program, triangleArrayB)
        .transformFeedback(transformFeedbackA);

        var currentDrawCall = drawCallA;
        function draw() {
            if (timer.ready()) {
                utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
            }

            timer.start();

            // DRAW
            app.clear();
            currentDrawCall.draw();
            
            // SWAP INPUT AND OUTPUT
            currentDrawCall = currentDrawCall === drawCallA ? drawCallB : drawCallA;

            timer.end();


            requestAnimationFrame(draw);
        }

        requestAnimationFrame(draw);

    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>
